using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FlatRedBall;
using FlatRedBall.Graphics;
using FlatRedBall.Input;
using FlatRedBall.Math.Geometry;

namespace MiJuego
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class GameComponentCollision : Microsoft.Xna.Framework.GameComponent
    {
        public Sprite sprite;
        public int iLayerCollision;
        public bool alive = true;

        public GameComponentCollision(Game game)
            : base(game)
        {
            this.iLayerCollision = 0;
        }

        public GameComponentCollision(Game game, int iLayerCollision)
            : base(game)
        {
            this.iLayerCollision = iLayerCollision;

            if (iLayerCollision > 0 && iLayerCollision < CollisionManager.getInstance().NLayer)
            {
                CollisionManager.getInstance().AddComponent(this);
            }
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            // TODO: Add your initialization code here

            FlatRedBall.Math.Geometry.AxisAlignedRectangle rectangle = new FlatRedBall.Math.Geometry.AxisAlignedRectangle();
            rectangle.ScaleX = sprite.ScaleX;
            rectangle.ScaleY = sprite.ScaleY;
            sprite.SetCollision(rectangle);

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            if (!alive)
            {
                CollisionManager.getInstance().RemoveComponent(this);

                SpriteManager.RemoveSprite(this.sprite);
                this.Game.Components.Remove(this);
            }

            base.Update(gameTime);
        }

        public virtual void Collision(GameComponentCollision component)
        {
            alive = false;
        }

        public void MoveTo(Vector3 target, float velocity)
        {
            Vector3 direction = target - sprite.Position;
            Vector3 direction2 = target - sprite.Position;
            direction.Normalize();

            sprite.XVelocity = velocity * direction.X;
            sprite.YVelocity = velocity * direction.Y;
        }

    }
}
